import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import jifen.bank.boc.util.FileUtil;
import util.DateUtil;

public class Test {
	public static List<String> readFileByLines(String fileName) {
        File file = new File(fileName);
        List<String> list = new ArrayList<>();
        BufferedReader reader = null;
        FileReader fileReader = null;
        try {
        	fileReader = new FileReader(file);
            reader = new BufferedReader(fileReader);
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                list.add(tempString);
            }
            return list;
        } catch (IOException e) {
        } finally {
        	if(fileReader != null){
        		try {
					fileReader.close();
				} catch (IOException e) {
				}
        	}
        	
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return null;
    }
	
	private static List<Map> parseFile(String filePath,int lengthArray[]){
	 	List<Map> ZmDataList = new ArrayList<Map>();
	 	try {
            String encoding="gbk"; //文件是gbk编码
            File file=new File(filePath);
            if(file.isFile() && file.exists()){ //判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                new FileInputStream(file), "iso-8859-1");
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while((lineTxt = bufferedReader.readLine()) != null){
                	byte[] bs = lineTxt.getBytes("iso-8859-1");
                	//解析的结果MAP，key为对账文件列序号，value为解析的值
        		 	Map<Integer,String> ZmDataMap = new LinkedHashMap<Integer,String>();
                    //左侧游标
                    int leftIndex = 0;
                    //右侧游标
                    int rightIndex = 0;
                    for(int i=0;i<lengthArray.length;i++){
                    	rightIndex = leftIndex + lengthArray[i];
                    	String filed = new String(Arrays.copyOfRange(bs, leftIndex,rightIndex), encoding);
                    	leftIndex = rightIndex+1;
                    	ZmDataMap.put(i, filed);
                    }
                    ZmDataList.add(ZmDataMap);
                }
                read.close();
        }else{
            System.out.println("找不到指定的文件");
        }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
	 	
		return ZmDataList;	
	}
	
	public static List<Map> parseZMFile(String filePath){
		int lengthArray[] = {3,11,11,6,10,19,12,4,2,21,2,32,2,6,10,13,13,4,15,2,2,6,2,4,32,1,21,15,1,15,32,13,13,8,32,13,13,12,2,1,32,98};
		return parseFile(filePath,lengthArray);
	}
	
	public static String getUUID(){ 
		String uuid = UUID.randomUUID().toString(); 
			//去掉“-”符号 
			return uuid.replaceAll("-", "");
	}
	
	  /**
     * 收集起始时间到结束时间之间所有的时间并以字符串集合方式返回
     * @param timeStart
     * @param timeEnd
     * @return
     */
    public static List<String> collectLocalDates(String timeStart, String timeEnd){
        return collectLocalDates(LocalDate.parse(timeStart), LocalDate.parse(timeEnd));
    }

    /**
     * 收集起始时间到结束时间之间所有的时间并以字符串集合方式返回
     * @param start
     * @param end
     * @return
     */
    public static List<String> collectLocalDates(LocalDate start, LocalDate end){
        // 用起始时间作为流的源头，按照每次加一天的方式创建一个无限流
        return Stream.iterate(start, localDate -> localDate.plusDays(1))
                     // 截断无限流，长度为起始时间和结束时间的差+1个
                     .limit(ChronoUnit.DAYS.between(start, end) + 1)
                     // 由于最后要的是字符串，所以map转换一下
                     .map(LocalDate::toString)
                     // 把流收集为List
                     .collect(Collectors.toList());
    }
	
	public static void main(String[] args) throws Exception {
//		String s = "6228 1100 9835 5969";
//		s = "a622c8+1100-9835+5b969";
//		s = s.replaceAll("[^0-9]", "");
//		System.out.println(s);
		
		
		  String timeStart = "2018-05-30";
	        String timeEnd = "2018-08-30";
//
	       List<String> result = collectLocalDates(timeStart, timeEnd);
	       StringBuilder data = new StringBuilder();
	       for(String date : result){
	    	   Date d = DateUtil.getDate(date, "yyyy-MM-dd");
	    	   Calendar cal=Calendar.getInstance();
	    	   cal.setTime(d);
	    	   if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.WEDNESDAY //周三
	    			|| cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY ||  cal.get(Calendar.DAY_OF_WEEK)== Calendar.SUNDAY ){//周六，周日
	    		 date = DateUtil.toString(cal.getTime(), "yyyy-MM-dd");
	    		 String sql = "INSERT INTO `jifen_product_bank_activity_places_stock` VALUES (null, 27, '"+date+" 00:00:00', '"+date+" 23:59:59', '500', NULL, NULL, NULL, NULL);";
		    	 data.append(sql).append("\n");
	    	   }
	    	   
	       }
	       String fileName = "F:\\data.txt";
	       FileUtil.saveFile(fileName, data.toString());
		
//		String activityEndDateStr = "2018-02-01";
//		Calendar calendar = Calendar.getInstance();
//		calendar.setTime(new Date());
//		calendar.add(Calendar.MINUTE, 2);		
//		Date activityEndDate = DateUtil.getDate(activityEndDateStr, DateUtil.FORMAT_DATE);
//		Calendar calendar2 = Calendar.getInstance();
//		calendar2.setTime(activityEndDate);
//		calendar2.add(Calendar.SECOND, -1);
//		activityEndDate = calendar2.getTime();
//		Date timeoutDate = calendar.getTime();
//		System.out.println(timeoutDate.before(activityEndDate) ? timeoutDate : activityEndDate);
//		String startDate = "2018-05-14 00:00:00";
//		System.out.println(DateUtil.getDate(startDate, DateUtil.FORMAT_DATE_TIME));
//		String date = DateUtil.toString(DateUtil.getDate(startDate, DateUtil.FORMAT_DATE_TIME), "yyyyMMdd");
//		
//		System.out.println(date);
//		String goodsInfo = "1|2|3a";
//		String[] goodsInfoArr = goodsInfo.split("\\|");
//		if(goodsInfoArr.length == 3){
//			String points = goodsInfoArr[2];
//			System.out.println(points);
//		}
//		
//		String amount = "";
//		System.out.println(amount.matches(Constant.CURRENCY_FEN_REGEX));
//		List<String> a = new ArrayList<String>();
//		a.add("1");
//		a.add("2");
//		for (String temp : a) {
//			if ("1".equals(temp)) {
//				a.remove(temp);
//			}
//		}
//		System.out.println(a);
		
//		LinkedHashMap<String, String> reqMap = new LinkedHashMap<>();
//		reqMap.put("a", "a");
//		reqMap.put("signAture", "");
//		reqMap.put("b", "b");
//		reqMap.put("signAture", "abc");
//		System.out.println(reqMap);
//		
		//System.out.println(getUUID());
//		Map<String,Object> m = new HashMap();
//		for(int i=0;i<20;i++){
//			m.put(i + "", "ab"+i);
//		}
//		System.out.println(m);
		//String s = "/tom/a/123/a.txt";
		//int start = s.lastIndexOf("/");  
		//System.out.println(s.substring(start + 1, s.length()));
		//String s = "C000000003187";
		//System.out.println(Integer.parseInt(s.substring(1, s.length())));
		
//		String card = "6221558812340000";
//		if(StringUtils.isNotBlank(card) && card.length() > 6){
//			card = card.substring(card.length()-6);
//		}
//		System.out.println(card);
//		
//		Calendar calendar = Calendar.getInstance(); // 当前日期
//		calendar.add(Calendar.MINUTE, 15);
//		System.out.println(calendar.getTime());
		
//		String ss = "gcRpjaFlDXxNrq/O5Dm1ZAAhPeZfQzrcVeWoaoU5+KhyT4j6NPyjjs5G909IP6fI/+V6+yM33u9JfYMj7KOvrhwHWvjXmm0EwmGXZg9jsPNCk2sE3YRDIIA2gPVYCBsYnevYkWZHazKUtzxrvFZl6obcyCbjMz8gOk9bZ9om0n0=";
//		String priKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJVGIOQQpfL8oFN75SaQD596rXMZYiAHGSBvPtoEkZvn54woqiINGgQfOUad3AUqbiZBY1+24w581yiDNikYpIR8Luyp8E8MewY9mLExbMvcnXTOQxNajowMhcoqowbU+B6rXAQ18mNxhJT96dhJmSDesVM5oFIUh36us+M+jtjjAgMBAAECgYABtnxKIabF0wBD9Pf8KUsEmXPEDlaB55LyPFSMS+Ef2NlfUlgha+UQhwsxND6CEKqS5c0uG/se/2+4l0jXz+CTYBEh+USYB3gxcMKEo5XDFOGaM2Ncbc7FAKJIkYYN2DHmr4voSM5YkVibw5Lerw0kKdYyr0Xd0kmqTok3JLiLgQJBAOGZ1ao9oqWUzCKnpuTmXre8pZLmpWPhm6S1FU0vHjI0pZh/jusc8UXSRPnx1gLsgXq0ux30j968x/DmkESwxX8CQQCpY1+2p1aX2EzYO3UoTbBUTg7lCsopVNVf41xriek7XF1YyXOwEOSokp2SDQcRoKJ2PyPc2FJ/f54pigdsW0adAkAM8JTnydc9ZhZ7WmBhOrFuGnzoux/7ZaJWxSguoCg8OvbQk2hwJd3U4mWgbHWY/1XB4wHkivWBkhRpxd+6gOUjAkBH9qscS52zZzbGiwQsOk1Wk88qKdpXku4QDeUe3vmSuZwC85tNyu+KWrfM6/H74DYFbK/MzK7H8iz80uJye5jVAkAEqEB/LwlpXljFAxTID/SLZBb+bCIoV/kvg+2145F+CSSUjEWRhG/+OH0cQfqomfg36WrvHl0g/Xw06fg31HgK";
//		String mobile = RSAUtils.getMobile(ss, priKey);
//		System.out.println(mobile);
//		String s = "jifen.bank.bcm.QueryPhoneCarrierApi";
//		System.out.println(s.length());
//		//File f = new File("F:\\wn\\wn\\业务资料\\中国银行卡券交易接口\\1.新增商户所需文档\\新增商户所需文档\\商户-华夏风云【HXFY】\\原始文件\\WARES.HXFY.20170525.00.P");
//		//getFileBianMa(f);
//		List<String> list = Arrays.asList("123", "45634", "7892", "abch", "sdfhrthj", "mvkd");  
//		list.stream().forEach(e ->{  
//		    if(e.equals("7892")){  
//		        return;  
//		    }  
//		    System.out.println(e);  
//		});
		//System.out.println(StringUtil.isBlank(" "));
	}
	
	public static void getFileBianMa(File f ){
		try{  
		  java.io.InputStream ios=new java.io.FileInputStream(f);  
		  byte[] b=new byte[3];  
		  ios.read(b);  
		  ios.close();  
		  if(b[0]==-17&&b[1]==-69&&b[2]==-65)  
		     System.out.println(f.getName()+"编码为UTF-8");  
		  else System.out.println(f.getName()+"可能是GBK");  
		}catch(Exception e){  
		   e.printStackTrace();  
		}

	}

}
